home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / WindowMarker.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  8KB  |  406 lines

  1. /*
  2. **    WindowMarker.c
  3. **
  4. **    Main window text block selection routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16. /****************************************************************************/
  17.  
  18. #define DB(x)    ;
  19.  
  20. /**********************************************************************/
  21.  
  22. STATIC struct MarkerContext *WindowMarkerContext;
  23. STATIC BOOL                     WindowMarkerInterrupted;
  24.  
  25. /**********************************************************************/
  26.  
  27.     /* Ask for the mouse position to be converted into a line and column index. */
  28.  
  29. STATIC VOID
  30. Window_AskPosition(struct MarkerContext *Context,LONG *Column,LONG *Line,WORD Mode)
  31. {
  32.     struct TextExtent Extent;
  33.     LONG Left,Top;
  34.  
  35.     Left    = Window->MouseX - Window->BorderLeft;
  36.     Top        = Window->MouseY - Window->BorderTop;
  37.  
  38.     if(Mode == MARKERASK_Scroll)
  39.     {
  40.         if(Top < 0)
  41.         {
  42.             *Line    = -1;
  43.             *Column    = 0;
  44.         }
  45.         else if(Top >= WindowHeight)
  46.         {
  47.             *Line    = Context->NumVisibleLines;
  48.             *Column    = Context->NumVisibleColumns - 1;
  49.         }
  50.         else
  51.         {
  52.             *Line = Top / Window->RPort->TxHeight;
  53.  
  54.             if(Left < 0)
  55.                 *Column = -1;
  56.             else if (Left >= WindowWidth)
  57.                 *Column = Context->NumVisibleColumns;
  58.             else
  59.                 *Column = TextFit(Window->RPort,&Raster[RasterWidth * (*Line)],LastColumn + 1,&Extent,NULL,1,Left,32767);
  60.         }
  61.     }
  62.     else
  63.     {
  64.         if(Top < 0)
  65.             Top = 0;
  66.         else if(Top >= WindowHeight)
  67.             Top = WindowHeight - 1;
  68.  
  69.         if(Left < 0)
  70.             Left = 0;
  71.         else if (Left >= WindowWidth)
  72.             Left = WindowWidth - 1;
  73.  
  74.         *Line    = Top / Window->RPort->TxHeight;
  75.         *Column = TextFit(Window->RPort,&Raster[RasterWidth * (*Line)],LastColumn + 1,&Extent,NULL,1,Left,32767);
  76.     }
  77.  
  78.     DB(kprintf("\033[0 p\033[HPosition     <%4ld,%4ld>\033[K\n",*Line,*Column));
  79. }
  80.  
  81.     /* Scroll the visible region. */
  82.  
  83. STATIC VOID
  84. Window_Scroll(struct MarkerContext *UnusedContext,LONG UnusedDeltaX,LONG UnusedDeltaY)
  85. {
  86. }
  87.  
  88.     /* Highlight a line of text between the "Left" and "Right" column (inclusive).
  89.      */
  90.  
  91. STATIC VOID
  92. Window_Highlight(struct MarkerContext *UnusedContext,LONG Line,LONG Left,LONG Right)
  93. {
  94.     if(Line >= 0 && Line <= LastLine)
  95.     {
  96.         if(Left >= 0 && Right <= LastColumn && Left <= Right)
  97.         {
  98.             struct RastPort *RPort;
  99.             LONG First,Length;
  100.  
  101.             RPort = Window->RPort;
  102.  
  103.             First    = TextLength(RPort,&Raster[RasterWidth * Line],Left);
  104.             Length    = TextLength(RPort,&Raster[RasterWidth * Line + Left],Right - Left + 1);
  105.  
  106.             if(Length > 0)
  107.             {
  108.                 ULONG OldAPen,OldBPen,OldDrMd;
  109.  
  110.                 OldAPen    = ReadAPen(RPort);
  111.                 OldBPen    = ReadBPen(RPort);
  112.                 OldDrMd    = ReadDrMd(RPort);
  113.  
  114.                 DB(kprintf("\033[2HMark    %4ld <%4ld,%4ld>\033[K\n",Line,Left,Right));
  115.  
  116.                 SetPens(RPort,(ULONG)~0,OldBPen,JAM1 | COMPLEMENT);
  117.                 FillWindowBox(Window,First,Line * RPort->TxHeight,Length,RPort->TxHeight);
  118.                 SetPens(RPort,OldAPen,OldBPen,OldDrMd);
  119.             }
  120.         }
  121.     }
  122. }
  123.  
  124.     /* Transfer the buffer contents line by line. */
  125.  
  126. STATIC BOOL
  127. Window_Put(APTR UserData,STRPTR Buffer,LONG Length)
  128. {
  129.     BOOL Result;
  130.  
  131.     if(Config->TerminalConfig->FontMode == FONT_STANDARD)
  132.         Result = (BOOL)(WriteChunkBytes(UserData,Buffer,Length) == Length);
  133.     else
  134.         Result = WriteTranslatedToClip(UserData,Buffer,Length);
  135.  
  136.     return(Result);
  137. }
  138.  
  139. STATIC BOOL
  140. Window_PutLine(APTR UserData,STRPTR Buffer,LONG Length)
  141. {
  142.     BOOL Result;
  143.  
  144.     if(Config->TerminalConfig->FontMode == FONT_STANDARD)
  145.         Result = (BOOL)(WriteChunkBytes(UserData,Buffer,Length) == Length);
  146.     else
  147.         Result = WriteTranslatedToClip(UserData,Buffer,Length);
  148.  
  149.     if(Result)
  150.         Result = (BOOL)(WriteChunkBytes(UserData,"\n",1) == 1);
  151.  
  152.     return(Result);
  153. }
  154.  
  155. STATIC BOOL
  156. Window_Transfer(struct MarkerContext *UnusedContext,LONG Line,LONG Left,LONG Right,MARKER_Put Put,APTR UserData)
  157. {
  158.     if(Line >= 0 && Line <= RasterHeight)
  159.     {
  160.         if(Left >= 0 && Right <= RasterWidth)
  161.             return(Put(UserData,&Raster[RasterWidth * Line + Left],Right - Left + 1));
  162.     }
  163.  
  164.     return(TRUE);
  165. }
  166.  
  167. STATIC BOOL
  168. Window_PickWord(struct MarkerContext *UnusedContext,LONG Left,LONG Top,LONG *WordLeft,LONG *WordRight)
  169. {
  170.     BOOL GotIt;
  171.  
  172.     GotIt = FALSE;
  173.  
  174.     SafeObtainSemaphoreShared(&RasterSemaphore);
  175.  
  176.     if(Left >= 0 && Left < RasterWidth && Top >= 0 && Top < RasterHeight)
  177.     {
  178.         if(Raster[RasterWidth * Top + Left] != ' ')
  179.         {
  180.             STRPTR RasterLine;
  181.             LONG From,To;
  182.  
  183.             From = To = Left;
  184.             RasterLine = &Raster[RasterWidth * Top];
  185.  
  186.             while(From > 0 && RasterLine[From - 1] != ' ')
  187.                 From--;
  188.  
  189.             while(To < RasterWidth - 1 && RasterLine[To + 1] != ' ')
  190.                 To++;
  191.  
  192.             GotIt = TRUE;
  193.  
  194.             *WordLeft    = From;
  195.             *WordRight    = To;
  196.         }
  197.     }
  198.  
  199.     ReleaseSemaphore(&RasterSemaphore);
  200.  
  201.     return(GotIt);
  202. }
  203.  
  204. STATIC APTR
  205. Window_TransferStartStop(struct MarkerContext *UnusedContext,APTR UserData,ULONG Qualifier)
  206. {
  207.     struct IFFHandle *Handle;
  208.  
  209.     if(Handle = UserData)
  210.     {
  211.         if(!PopChunk(Handle))
  212.             PopChunk(Handle);
  213.     }
  214.     else
  215.     {
  216.         APTR Buffer;
  217.         LONG Size;
  218.  
  219.         SafeObtainSemaphoreShared(&RasterSemaphore);
  220.  
  221.         SetWait(Window);
  222.  
  223.         Buffer    = NULL;
  224.         Size    = 0;
  225.  
  226.         if(Qualifier & SHIFT_KEY)
  227.             GetClipContents(Config->ClipConfig->ClipboardUnit,&Buffer,&Size);
  228.  
  229.         if(Handle = OpenIFFClip(Config->ClipConfig->ClipboardUnit,MODE_NEWFILE))
  230.         {
  231.             if(!PushChunk(Handle,ID_FTXT,ID_FORM,IFFSIZE_UNKNOWN))
  232.             {
  233.                 if(!PushChunk(Handle,0,ID_CHRS,IFFSIZE_UNKNOWN))
  234.                 {
  235.                     if(Size > 0)
  236.                     {
  237.                         BOOL Ok;
  238.  
  239.                         Ok = (BOOL)(WriteChunkBytes(Handle,Buffer,Size) == Size);
  240.  
  241.                         FreeVecPooled(Buffer);
  242.  
  243.                         if(Ok)
  244.                             return(Handle);
  245.                     }
  246.                     else
  247.                         return(Handle);
  248.                 }
  249.             }
  250.         }
  251.     }
  252.  
  253.     CloseIFFClip(Handle);
  254.  
  255.     ReleaseSemaphore(&RasterSemaphore);
  256.  
  257.     ClrWait(Window);
  258.  
  259.     return(NULL);
  260. }
  261.  
  262. VOID
  263. WindowMarkerStop()
  264. {
  265.     if(WindowMarkerContext)
  266.     {
  267.         UpMarker(WindowMarkerContext);
  268.         Marking = FALSE;
  269.  
  270.         FreeVecPooled(WindowMarkerContext);
  271.         WindowMarkerContext = NULL;
  272.  
  273.         ReportMouse(FALSE,Window);
  274.     }
  275.  
  276.     OffItem(MEN_COPY);
  277.     OffItem(MEN_CLEAR);
  278. }
  279.  
  280. VOID
  281. WindowMarkerSelectAll()
  282. {
  283.     WindowMarkerStop();
  284.  
  285.     if(WindowMarkerContext = CreateMarkerContext(
  286.         Window_AskPosition,
  287.         Window_Scroll,
  288.         Window_Highlight,
  289.         Window_Highlight,
  290.         Window_TransferStartStop,
  291.         Window_Transfer,
  292.         Window_Put,
  293.         Window_PutLine,
  294.         Window_PickWord
  295.     ))
  296.     {
  297.         SelectAllMarker(
  298.             WindowMarkerContext,0,LastLine + 1,LastLine + 1,
  299.  
  300.             0,LastColumn + 1,LastColumn + 1,
  301.  
  302.             0,0,LastColumn,LastLine
  303.         );
  304.  
  305.         Marking = TRUE;
  306.         WindowMarkerInterrupt();
  307.     }
  308. }
  309.  
  310. VOID
  311. WindowMarkWord()
  312. {
  313.     WindowMarkerStop();
  314.  
  315.     if(WindowMarkerContext = CreateMarkerContext(
  316.         Window_AskPosition,
  317.         Window_Scroll,
  318.         Window_Highlight,
  319.         Window_Highlight,
  320.         Window_TransferStartStop,
  321.         Window_Transfer,
  322.         Window_Put,
  323.         Window_PutLine,
  324.         Window_PickWord
  325.     ))
  326.     {
  327.         if(!SetWordMarker(WindowMarkerContext,0,LastLine + 1,LastLine + 1,0,LastColumn + 1,LastColumn + 1))
  328.             WindowMarkerStop();
  329.         else
  330.         {
  331.             Marking = TRUE;
  332.             WindowMarkerInterrupt();
  333.         }
  334.     }
  335. }
  336.  
  337. VOID
  338. WindowMarkerStart(ULONG MsgQualifier)
  339. {
  340.     if(WindowMarkerContext && !(MsgQualifier & SHIFT_KEY))
  341.         WindowMarkerStop();
  342.  
  343.     WindowMarkerInterrupted = FALSE;
  344.  
  345.     if(WindowMarkerContext)
  346.         MoveMouseMarker(WindowMarkerContext);
  347.     else
  348.     {
  349.         if(WindowMarkerContext = CreateMarkerContext(
  350.             Window_AskPosition,
  351.             Window_Scroll,
  352.             Window_Highlight,
  353.             Window_Highlight,
  354.             Window_TransferStartStop,
  355.             Window_Transfer,
  356.             Window_Put,
  357.             Window_PutLine,
  358.             Window_PickWord
  359.         ))
  360.         {
  361.             DownMarker(WindowMarkerContext,0,LastLine + 1,LastLine + 1,0,LastColumn + 1,LastColumn + 1);
  362.             Marking = TRUE;
  363.         }
  364.     }
  365.  
  366.     if(Marking)
  367.         ReportMouse(TRUE,Window);
  368. }
  369.  
  370. VOID
  371. WindowMarkerInterrupt()
  372. {
  373.     if(CheckMarker(WindowMarkerContext))
  374.     {
  375.         OnItem(MEN_COPY);
  376.         OnItem(MEN_CLEAR);
  377.     }
  378.     else
  379.     {
  380.         OffItem(MEN_COPY);
  381.         OffItem(MEN_CLEAR);
  382.     }
  383.  
  384.     WindowMarkerInterrupted = TRUE;
  385.  
  386.     ReportMouse(FALSE,Window);
  387. }
  388.  
  389. VOID
  390. WindowMarkerMoveMouse()
  391. {
  392.     if(WindowMarkerContext && !WindowMarkerInterrupted)
  393.         MoveMouseMarker(WindowMarkerContext);
  394. }
  395.  
  396. VOID
  397. WindowMarkerTransfer(ULONG MsgQualifier)
  398. {
  399.     if(WindowMarkerContext)
  400.     {
  401.         TransferMarker(WindowMarkerContext,MsgQualifier);
  402.  
  403.         WindowMarkerStop();
  404.     }
  405. }
  406.